package com.jffccloud.common.redis;

import java.util.HashSet;
import java.util.List;

import com.jffccloud.common.util.SerializerUtil;
import com.jffccloud.common.util.SpringContextHolder;
import com.jfinal.plugin.redis.IKeyNamingPolicy;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

@Configuration
public class JedisConfiguration {

    @Autowired
    private JedisProperties jedisProperties;

    @Bean
    @ConditionalOnProperty(name = "jffccloud.redis", havingValue = "pool", matchIfMissing = true)
    public JedisPool getJedisPool() {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        JedisPool jedisPool = new JedisPool(poolConfig, jedisProperties.getHost(), jedisProperties.getPort(),
                jedisProperties.getTimeout(), jedisProperties.getPassword(), jedisProperties.getSsl());
        return jedisPool;
    }

    @Bean
    @ConditionalOnProperty(name = "jffccloud.redis", havingValue = "cluster", matchIfMissing = true)
    public JedisCluster getJedisCluster() {
        HashSet<HostAndPort> nodesSet = new HashSet<>();
        if(jedisProperties.getCluster()!=null){
            List<String> nodesList = jedisProperties.getCluster().getNodes();
            if (nodesList != null && nodesList.size() > 0) {
                for (String node : nodesList) {
                    if (node != null) {
                        String[] hostAndPort = node.split(":");
                        HostAndPort hostAndPort2 = new HostAndPort(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
                        nodesSet.add(hostAndPort2);
                    }
                }
            }
        }
        return new JedisCluster(nodesSet);
    }

    @Bean
    public IKeyNamingPolicy IKeyNamingPolicyStart() {
        return IKeyNamingPolicy.defaultKeyNamingPolicy;
    }

    @Bean
    @ConditionalOnProperty(name = "jffccloud.redis", havingValue = "pool", matchIfMissing = true)
    public JedisClientPool getJedisClientPool(IKeyNamingPolicy keyNamingPolicy,SerializerUtil serializer){
        JedisPool jedisPool = SpringContextHolder.getBean(JedisPool.class);
        return new JedisClientPool(jedisPool,keyNamingPolicy,serializer);
    }

    @Bean
    @ConditionalOnProperty(name = "jffccloud.redis", havingValue = "cluster", matchIfMissing = true)
    public JedisClientCluster getJedisClientCluster(IKeyNamingPolicy keyNamingPolicy,SerializerUtil serializer){
        JedisCluster jedisPool = SpringContextHolder.getBean(JedisCluster.class);
        return new JedisClientCluster(jedisPool,keyNamingPolicy,serializer);
    }
}
